package com.dabaozhan.modules.wx_app.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.aliyun.sms.SmsMessage;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.SmsEntity;
import com.dabaozhan.modules.wx_app.entity.UserEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author houzhong
 * @date 2019-01-10 15:43
 */

@Api(value = "短信", description = "短信接口")
@RestController
@RequestMapping("/wx_app")
public class SmsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsController.class);

    /**短信*/
    @Autowired
    private SmsService smsService;

    @ApiOperation(value = "获取登录短信注册码")
    @RequestMapping(value = "/obtainMessageLogin")
    public String obtainMessageLogin(UserEntity v) {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(4);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_155270574");
                sms.setStatus(4);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("获取登录短信注册码发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("获取登录短信注册码请求参数:{}-返回结果:{}", JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "获取短信注册码")
    @RequestMapping(value = "/obtainMessage")
    public String obtainMessage(UserEntity v) {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(3);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_154593773");
                sms.setStatus(3);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("获取短信验证发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("获取短信验证请求参数:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "修改手机号获取短信注册码")
    @RequestMapping(value = "/obtainMessagePhone")
    public String obtainMessagePhone(UserEntity v) throws Exception {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(1);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_144450236");
                sms.setStatus(1);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("修改手机号获取短信注册码发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("修改手机号获取短信注册码请求参数:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "设置支付密码获取验证码")
    @RequestMapping(value = "/obtainMessagePass")
    public String obtainMessagePass(UserEntity v) throws Exception {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(2);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_155295178");
                sms.setStatus(2);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("设置支付密码获取验证码发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("设置支付密码获取验证码请求参数:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "绑定银行卡获取验证码")
    @RequestMapping(value = "/obtainMessageBank")
    public String obtainMessageBank(UserEntity v) throws Exception {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(5);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_155295178");
                sms.setStatus(5);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("绑定银行卡发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("绑定银行卡请求参数:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }

    @ApiOperation(value = "忘记密码获取验证码")
    @RequestMapping(value = "/obtainMessagePassword")
    public String obtainMessagePassword(UserEntity v) throws Exception {
        ResponseDto<String> responseDto = new ResponseDto<String>();
        try {
            SmsEntity smsVo = new SmsEntity();
            smsVo.setPhone(v.getTelephone());
            smsVo.setStatus(6);
            Integer count = smsService.findBySmsCount(smsVo);
            if (count < 5) {
                SmsEntity sms = SmsMessage.sendSms(v.getTelephone(),"SMS_157330043");
                sms.setStatus(6);
                smsService.saveSms(sms);
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
                responseDto.setResult(sms.getCode());
            }else {
                responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_204);
            }
        } catch(RRException re){
            responseDto.setCode(re.getCode());
            responseDto.setMessage(re.getMessage());
        } catch (ClientException e) {
            responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
            //记录异常日志
            LOGGER.error("忘记密码获取验证码发生异常,异常详情如下:",e);
        } finally {
            //记录请求入参和返回日志
            LOGGER.info("忘记密码获取验证码请求参数:{}-返回结果:{}",JSON.toJSONString(v),JSON.toJSONString(responseDto));
        }
        return JSON.toJSONString(responseDto);
    }
}
